// pages/distribution/distribution.js
const app = getApp()
var utils = require('../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curPage:1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
		var data = new Date;
		var month = (data.getMonth() + 1);
		console.log(data.getFullYear(), (data.getMonth() + 1), data.getDate())
		month = month < 10 ? '0' + month : month
		var day = new Date(data.getFullYear(), month, 0) //获取当月的最后一天
		//	console.log(day.getDate())

		that.setData({
			startdate: data.getFullYear() + "-" + (data.getMonth() + 1) + "-" + 1,
			enddate: data.getFullYear() + "-" + (data.getMonth() + 1) + "-" + day.getDate()
		})
        that.getUserInfo();
		that.getComlist();
    },
    getUserInfo:function(){
        var that = this;
        utils.http("/api/v1/member/index", {
        }, function (res) {
            if (res.state == 1) {
                that.setData({
                    user_info: res.datas
                })
            } else {
                utils.message(res.datas)
            }
        })
    },
	onDetilsTap:function(){
		wx.navigateTo({
			url: '../detils/detils',
			success: function(res) {},
			fail: function(res) {},
			complete: function(res) {},
		})
	},
	onMemberVipTap:function(){
		var that = this;
		utils.http("/api/v1/member/apply_next_grades", {
		}, function (res) {
			if (res.state == 1) {
				that.getUserInfo();
			} else {
				utils.message(res.datas)
			}
		})
	},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    //结算记录
    onDetailedTap(){
        wx.navigateTo({
            url: '../detailed/detailed'
        })
    },
	onVipDetailsTap(){
		wx.navigateTo({
			url: '../vipDetails/vipDetails',
			success: function(res) {},
			fail: function(res) {},
			complete: function(res) {},
		})
	},
    startDateChange: function (e) {
        var that = this;
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            startdate: e.detail.value,
            curPage:1,
            com_list:[],
        })
        that.getComlist();
    },

    endDateChange: function (e) {
        var that = this;
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            enddate: e.detail.value,
            curPage:1,
            com_list:[],
        })
        that.getComlist();
    },
    getComlist:function(){
        var that = this;
        if(that.data.startdate == undefined || that.data.enddate == undefined){
            utils.showTishi('开始日期和结束日期都需要选择哦');
            return false;
        }
        var apiUrl = '/api/v1/member/commission_log';
        var apiData = {
            page:that.data.curPage,
            s_date:that.data.startdate,
            e_date:that.data.enddate,
        }
        utils.http(apiUrl,apiData,function(res){
            if (res.state == 1) {
                that.setData({
                    order_amount:res.datas.amount
                });
                var com_list = that.data.com_list || [];
                for (var i in res.datas.list) {
                    com_list.push(res.datas.list[i])
                }
                if (res.datas.list.length < 10) {
                    that.setData({
                        tishi_title: '已加载全部',
                    });
                } else {
                    that.setData({
                        tishi_title: '正在加载中...',
                    });
                }
                that.setData({
                    com_list: com_list,
                    ready: res.datas.list.length == 0 ? 1 : 2,
                })
            } else {
                utils.message(res.datas)
            }
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.curPage + 1
            that.setData({
                curPage: page,
            })
            that.getComlist();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})